package com.zzyl.controller.security;

import com.zzyl.base.PageBean;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PostMapping("page/{pageNum}/{pageSize}")
    @ApiOperation(value = "角色分页", notes = "角色分页")
    public ResponseResult<PageBean<RoleVo>> selectPage(@PathVariable("pageNum") Integer pageNum,
                                                       @PathVariable("pageSize") Integer pageSize,
                                                       @RequestBody RoleDto roleDto) {
        log.info("角色分页查询，pageNum={},pageSize={},roleDto={}", pageNum, pageSize, roleDto);
        PageBean<RoleVo> pageBean= roleService.selectPage(roleDto, pageNum, pageSize);
        return ResponseResult.success(pageBean);
    }


    @ApiOperation("角色添加")
    @PutMapping
    public ResponseResult<Void> addRole(@RequestBody RoleDto roleDto) {
        log.info("角色添加，roleDto={}", roleDto);
        roleService.addRole(roleDto);
        return ResponseResult.success();
    }

    @ApiOperation("根据角色查询选中的资源数据")
    @GetMapping("find-checked-resources/{roleId}")
    public ResponseResult<Set<String>> findCheckedResources(@PathVariable Long roleId) {
        log.info("根据角色查询选中的资源数据，roleId={}", roleId);
        Set<String> resources = roleService.findCheckedResources(roleId);
        return ResponseResult.success(resources);
    }

    @ApiOperation("角色修改")
    @PatchMapping
    public ResponseResult<Void> updateRole(@RequestBody RoleDto roleDto) {
        log.info("角色修改，roleDto={}", roleDto);
        roleService.updateRole(roleDto);
        return ResponseResult.success();
    }


    @ApiOperation("删除角色")
    @DeleteMapping("{roleIds}")
    public ResponseResult<Void> deleteRole(@PathVariable Long[] roleIds) {
        log.info("删除角色，roleIds={}", roleIds);
        roleService.deleteRole(roleIds);
        return ResponseResult.success();
    }


    @ApiOperation("查询所有角色")
    @PostMapping("init-roles")
    public ResponseResult<List<RoleVo>> list() {
        return ResponseResult.success(roleService.list());
    }
}
